package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.DrugStorageBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.DrugStorageMapper;
import com.java2207.group1.pojo.DrugStorage;
import com.java2207.group1.service.DrugStorageService;
import com.java2207.group1.vo.drugstorage.DrugPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Lenovo 2022/7/26 12:07
 */
@Service("drugStorageService")
public class DrugStorageServiceImpl implements DrugStorageService {


    @Autowired
    DrugStorageMapper drugStorageMapper;

    @Override
    public LayuiPageVo<DrugStorage> drugLayuiPageData(Map<String, Object> params) {
        //把page和limit转从Object换成int类型
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");
        // mybatis 的 分页插件
        PageHelper.startPage(page, limit);

        List<DrugStorage> drugStorages = drugStorageMapper.selectAllDrugByParams(params);

        // mybatis 的 分页数据结构
        PageInfo<DrugStorage> drugPageInfo = new PageInfo<>(drugStorages);

        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中
        LayuiPageVo<DrugStorage> drugLayuiPageVo = new LayuiPageVo<>();
        drugLayuiPageVo.setCode(0);                         // layui 默认 0 是正确的状态码
        drugLayuiPageVo.setMsg("分页列表数据");               // 提示信息
        drugLayuiPageVo.setCount(drugPageInfo.getTotal());       // 总记录数
        drugLayuiPageVo.setData(drugPageInfo.getList());        // 分页的列表数据

        return drugLayuiPageVo;
    }

    @Override
    public int addDrug(DrugStorage drug) {

        return drugStorageMapper.addDrug(drug);
    }

    @Override
    public int deleteDrug(Integer id) {
        return drugStorageMapper.deleteDrug(id);
    }


    @Override
    public int updateDrug(DrugStorage drug) {
        return drugStorageMapper.updateDrug(drug);
    }

    @Override
    public int deleteDrugByIds(String ids) {
        String[] arr = ids.split(",");
        return drugStorageMapper.deleteDrugByIds(arr);
    }

    @Override
    public int reduceDrug(DrugStorage drug) {

        return drugStorageMapper.reduceDrug(drug);
    }

    @Override
    public List<DrugStorage> selectAllDrug() {
        return drugStorageMapper.selectAllDrug();
    }

    @Override
    public List<DrugPieVo> drugPieData() {
        List<DrugPieVo> list = drugStorageMapper.drugPieData();
        return list;
    }


    @Override
    public List<DrugStorageBo> druExcelData(Map<String, Object> params) {

        List<DrugStorage> drugStorages = drugStorageMapper.selectAllDrugByParams(params);

        List<DrugStorageBo> drugBos = drugStorages.stream().map((dru) ->{
            DrugStorageBo drugBo = new DrugStorageBo();
            drugBo.setId(dru.getId());
            drugBo.setName(dru.getName());
            drugBo.setPlaceorigin(dru.getPlaceorigin());
            drugBo.setEffect(dru.getEffect());
            drugBo.setInventory(dru.getInventory());
            drugBo.setPic(dru.getPic());
            return drugBo;
        }).collect(Collectors.toList());

        return drugBos;
    }


}
